package com.rock.dom;

import com.rock.Office;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.util.ArrayList;
import java.util.List;

public class CreateDom {

    public static void main(String[] args) throws ParserConfigurationException, TransformerException {
        List<Office> list = new ArrayList<Office>();

        Office milkyWay = new Office();
        milkyWay.setAddr("龙华区");
        milkyWay.setArea(1000);
        milkyWay.setName("星河办公室");
        milkyWay.setEmployeesNum(300);
        milkyWay.setFloor(3);
        list.add(milkyWay);

        Office southMountain = new Office();
        southMountain.setAddr("南山区");
        southMountain.setArea(2500);
        southMountain.setName("南山办公室");
        southMountain.setEmployeesNum(800);
        southMountain.setFloor(47);
        list.add(southMountain);


        //创建DocumentBuilderFactory对象
        DocumentBuilderFactory db=DocumentBuilderFactory.newInstance();
        //创建DocumentBuilder对象
        DocumentBuilder b=db.newDocumentBuilder();
        //创建document对象
        Document dom=b.newDocument();
        //定义根节点
        Element root = dom.createElement("info");
        //添加根节点到dom树
        dom.appendChild(root);

        for (Office office : list) {
            //设置一个主标签
            Element offElement = dom.createElement("office");
            offElement.setAttribute("floor", String.valueOf(office.getFloor()));
            root.appendChild(offElement);

            //设置name子标签
            Element nameElement = dom.createElement("name");
            nameElement.setTextContent(office.getName());
            offElement.appendChild(nameElement);

            //设置addr子标签
            Element addrElement = dom.createElement("addr");
            addrElement.setTextContent(String.valueOf(office.getAddr()));
            offElement.appendChild(addrElement);

            //设置area子标签
            Element areaElement = dom.createElement("area");
            areaElement.setTextContent(String.valueOf(office.getArea()));
            offElement.appendChild(areaElement);

//            //设置floor子标签
//            Element floorElement = dom.createElement("floor");
//            floorElement.setTextContent(String.valueOf(office.getFloor()));
//            offElement.appendChild(floorElement);
        }

        //通过工厂实例创建transformer类实例
        TransformerFactory tf=TransformerFactory.newInstance();
        Transformer tans=tf.newTransformer();

        //美化xml
        tans.setOutputProperty(OutputKeys.INDENT, "yes");
        tans.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2");

        //以 Document Object Model（DOM）树的形式充当转换 Source 树的持有者。
        DOMSource ds=new DOMSource(dom);
        StreamResult result=new StreamResult("D:\\code\\homework\\Monday\\src\\resource\\office.xml");

        //将 XML Source 转换为 Result
        tans.transform(ds, result);
    }
}
